<?php
class URL {
	private $_SOCKET = NULL;
	public function __construct() {
		$this->_SOCKET = NULL;
	}
	function REQUEST($getURL, $getMethod = "GET", $getDataArr = array(), $cookierArr = array()) {
		// настройка адреса
		$_URL = parse_url ( $getURL );
		if (! isset ( $_URL ["host"] )) {
			return FALSE;
		}
		if (! isset ( $_URL ["port"] )) {
			$_URL ["port"] = 80;
		}
		if (! isset ( $_URL ["path"] )) {
			$_URL ["path"] = "/";
		}
		if (isset ( $_URL ["query"] )) {
			$_URL ["path"] .= "?" . $_URL ["query"];
		}
		if ($getDataArr && $getMethod == "POST") {
			$getDataArr = http_build_query ( $getDataArr );
		} else {
			$data = false;
		}
		// соединение
		$i = 0;
		do {
			$this->_SOCKET = socket_create ( AF_INET, SOCK_STREAM, SOL_TCP );
			if (! $this->_SOCKET) {
				echo "Ошибка создания соккета - " . $i . "!";
				sleep ( 2 );
			}
			$i ++;
			if ($i == 5) {
				return FALSE;
			}
		} while ( ! $this->_SOCKET );
		do {
			$bConnect = socket_connect ( $this->_SOCKET, $_URL ["host"], $_URL ["port"] );
			if (! $bConnect) {
				echo "Ошибка установки связи - " . $i . "!";
				sleep ( 2 );
			}
			$i ++;
			if ($i == 5) {
				return FALSE;
			}
		} while ( ! $bConnect );
		// Если установить флаг до socket_connect соединения не происходит
		// отправка запроса
		$_WRITE = socket_write ( $this->_SOCKET, $getMethod . " " . $_URL ["path"] . " HTTP/1.1\r\n" );
		$_WRITE = socket_write ( $this->_SOCKET, "Host: " . $_URL ["host"] . "\r\n" );
		$_WRITE = socket_write ( $this->_SOCKET, "Connection: close\r\n" );
		if ($cookierArr) {
			foreach ( $cookierArr as $o => $v ) {
				$_WRITE = socket_write ( $this->_SOCKET, "Cookie: " . $o . "=" . $v . "\r\n" );
			}
			$_WRITE = socket_write ( $this->_SOCKET, "Accept: */*\r\n" );
		}
		if ($data) {
			socket_write ( $this->_SOCKET, "Content-Type: application/x-www-form-urlencoded\r\n" );
			socket_write ( $this->_SOCKET, "Content-length: " . strlen ( $getDataArr ) . "\r\n" );
			socket_write ( $this->_SOCKET, "\r\n" );
			socket_write ( $this->_SOCKET, $data . "\r\n" );
		}
		$_WRITE = socket_write ( $this->_SOCKET, "\r\n" );
		// чтение ответа
		$requestPage = "";
		do {
			$read = socket_recv ( $this->_SOCKET, $request, 4096, 0 );
			if ($read != FALSE) {
				$requestPage .= $request;
			}
		} while ( $read != FALSE );
		$this->Close ();
		return $this->Interpretation ( $requestPage );
	}
	function GET($getURL, $cookierArr = array()) {
		return $this->REQUEST ( $getURL, "GET", NULL, $cookierArr );
	}
	function POST($getURL, $getDataArr = array()) {
		return $this->REQUEST ( $getURL, "POST", $getDataArr );
	}
	function __destruct() {
		$this->Close ();
	}
	protected function Close() {
		if ($this->_SOCKET) {
			socket_close ( $this->_SOCKET );
			$this->_SOCKET = NULL;
		}
	}
	function Interpretation($requestPage) {
		$requestPage = str_replace ( array ("\n", "\r", "\t" ), "", $requestPage );
		return $requestPage;
	}
}
?>